// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: implementation of CXFormsParseLoadURI.
//   $Id$


#include "stdafx.h"

#include "ImplContextualValue.h"
#include "ActionLoad.h"
#include "parseLoad.h"
#include "tags.h"


CMapStringToPtr CXFormsParseLoadURI::sm_ShowTokens;
HANDLE CXFormsParseLoadURI::sm_hShowTokensMutex = 0;

CXFormsParseLoadURI::CXFormsParseLoadURI(CImplActionLoadURI* pImpl): CXFormsParseAction((CImplAction*) pImpl)
{
	if(!sm_hShowTokensMutex)
		sm_hShowTokensMutex = CreateMutex(0, FALSE, _T("Local\\XFormsParseActionShowTokensMutex"));

	WaitForSingleObject(sm_hShowTokensMutex, INFINITE);

	try
	{
		if(sm_ShowTokens.IsEmpty())
		{
			FillTokenizer();
		}
		addAttributeCheck(ATTR_SHOW,(LPVOID*)&pImpl->m_eShow,&sm_ShowTokens);
		addAttributeCheck(ATTR_LOADHREF,&pImpl->m_sHref);
		addAttributeCheck(ATTR_MODEL, &pImpl->m_sModel);
		addAttributeCheck(ATTR_REF,&pImpl->m_sRef);
		addAttributeCheck(ATTR_BIND,&pImpl->m_sBind);
		//action attributes (easier than multiple inheritance)
		addAttributeCheck(ATTR_EVENT, &pImpl->m_sEvent);
		addAttributeCheck(ATTR_OBSERVER, &pImpl->m_sObserver);
		m_bCheckElements = true;
	}
	catch(...)
	{
	}

	ReleaseMutex(sm_hShowTokensMutex);
};

void CXFormsParseLoadURI::CheckElement(IHTMLElement *pElem, BSTR bstrTag, CImpl* pImpl)
{
	if (wcscmp(bstrTag, L"resource") == 0)
	{
		CImplContextualValue * pCV = new CImplContextualValue();
		CComVariant vtTemp;
		pElem->getAttribute(L"value",1,&vtTemp);
		if(vtTemp.vt == VT_BSTR)
			vtTemp.CopyTo(&pCV->m_sValueExpression);
		else
			 pElem->get_innerHTML(&pCV->m_sSimpleValue);
		CImplActionLoadURI * pLoad = dynamic_cast<CImplActionLoadURI*>(pImpl);
		if(pLoad)
			pLoad->m_pResourceResolver = pCV;
	}
}

//Tokenisers
void CXFormsParseLoadURI::FillTokenizer()
{
	//these are a superset, only new and replace exist now.
	sm_ShowTokens.SetAt(_T("new"),		(LPVOID)CImplActionLoadURI::EXlinkShow::_new);
	sm_ShowTokens.SetAt(_T("replace"),	(LPVOID)CImplActionLoadURI::EXlinkShow::replace);
	sm_ShowTokens.SetAt(_T("embed"),	(LPVOID)CImplActionLoadURI::EXlinkShow::embed);
	sm_ShowTokens.SetAt(_T("other"),	(LPVOID)CImplActionLoadURI::EXlinkShow::other);
	sm_ShowTokens.SetAt(_T("none"),		(LPVOID)CImplActionLoadURI::EXlinkShow::none);

	// Correct syntax for systray/softbar invocation:
	sm_ShowTokens.SetAt(_T("fp:softbar"), (LPVOID)CImplActionLoadURI::EXlinkShow::softbar);
	sm_ShowTokens.SetAt(_T("fp:systray"), (LPVOID)CImplActionLoadURI::EXlinkShow::systray);
	// Deprecated syntax, stil supported for backwards compatability.
	sm_ShowTokens.SetAt(_T("softbar"), (LPVOID)CImplActionLoadURI::EXlinkShow::softbar);
	sm_ShowTokens.SetAt(_T("systray"), (LPVOID)CImplActionLoadURI::EXlinkShow::systray);
}